//* upgrade buttons

// button images
buttons = new Array();
buttons[0] = new Image();
buttons[0].src = "img/button/range.png";
buttons[0].alt = "Range";
buttons[0].setAttribute("cost","200");
buttons[0].setAttribute("increase","20");

buttons[1] = new Image();
buttons[1].src = "img/button/spread.png";
buttons[1].alt = "Spread";
buttons[1].setAttribute("cost","250");
buttons[1].setAttribute("increase","1");

buttons[2] = new Image();
buttons[2].src = "img/button/rate.png";
buttons[2].alt = "Rate";
buttons[2].setAttribute("cost","280");
buttons[2].setAttribute("increase","50");

buttons[3] = new Image();
buttons[3].src = "img/button/speed.png";
buttons[3].alt = "Speed";
buttons[3].setAttribute("cost","300");
buttons[3].setAttribute("increase","2");

buttons[4] = new Image();
buttons[4].src = "img/button/magnet.png";
buttons[4].alt = "Magnet";
buttons[4].setAttribute("cost","500");
buttons[4].setAttribute("increase","10");

buttons[5] = new Image();
buttons[5].src = "img/button/life.png";
buttons[5].alt = "Life";
buttons[5].setAttribute("cost","700");
buttons[5].setAttribute("increase","100");

buttons[6] = new Image();
buttons[6].src = "img/button/credit.png";
buttons[6].alt = "Credit";
buttons[6].setAttribute("cost","5000");
buttons[6].setAttribute("increase","1");

buttons[7] = new Image();
buttons[7].src = "img/button/penetration.png";
buttons[7].alt = "Penetration";
buttons[7].setAttribute("cost","1000");
buttons[7].setAttribute("increase","1");

buttons[8] = new Image();
buttons[8].src = "img/button/beam.png";
buttons[8].alt = "Beams";
buttons[8].setAttribute("cost","12000");
buttons[8].setAttribute("increase","1");

button_index = -1;

function buttonsDraw() {
  //* draw buttons
  var k = 34;
  for (var i=0; i<buttons.length; i++) {
    // detect if mouse is near buttons, then they must be wider for help
    var l = 0;
    if ( (Math.abs((100+30*i-4) - my + 12) < 16) && (mx < 40) ) {
      l = 110;
    } 
    // blue background, gray if not enough credits
    if (buttons[i].getAttribute("cost") <= ship.credit)
      context.fillStyle = "rgba(0,255,0,0.3)";
    else
      context.fillStyle = "rgba(255,255,255,0.2)";
    context.fillRect(4,100+30*i-4,35+l,25);
    // shortcut key
    context.fillStyle = "white";
    context.fillText(i+1,8,100+30*i+12);
    // current value
    s = ' ?';
    if (buttons[i].alt == "Range")       s = ship.range+'00 km';
    if (buttons[i].alt == "Spread")      s = ship.spread+'x';
    if (buttons[i].alt == "Rate")        s = Math.round(ship.rate)+' rpm';
    if (buttons[i].alt == "Speed")       s = ship.max_speed*2+' km/s';
    if (buttons[i].alt == "Magnet")      s = ship.magnet+'0 km';
    if (buttons[i].alt == "Life")        s = ship.life+' %';
    if (buttons[i].alt == "Credit")      s = ship.credit_multiplier+'x';
    if (buttons[i].alt == "Penetration") s = ship.penetration+'x';
    if (buttons[i].alt == "Beams")       s = ship.beams+'x';
    // hint
    if (l > 0) {
      button_index = i;
      context.fillText(buttons[i].alt+' '+s,45,100+30*i+7);
      context.fillText('$ '+buttons[i].getAttribute("cost"),45,100+30*i+18);
    }
    // bitmap
    context.drawImage(buttons[i],18,100+30*i);
  }
}

function buttonDetect(ALeft,ATop) {
  // detect click on mouse buttons
  if (ALeft > 40)
    return;
  if (ATop < 95)
    return;
  buySomething(button_index);
}

function buySomething(AIndex) {
  // buy item defined by button index, 0 is range, 7 is penetration
  if (ship.life <= 0)
    return;
  var cost = buttons[AIndex].getAttribute('cost');
  var increase = 1*buttons[AIndex].getAttribute('increase');  
  if (ship.credit >= cost) {
    ship.credit -= cost;
    switch (AIndex) {
      case 0: ship.range += increase; break; 
      case 1: ship.spread += increase; break; 
      case 2: ship.rate += increase; break; 
      case 3: ship.max_speed += increase; break; 
      case 4: ship.magnet += increase; break; 
      case 5: ship.life += increase; break; 
      case 6: ship.credit_multiplier += increase; break; 
      case 7: ship.penetration += increase; break; 
      case 8: ship.beams += increase; break; 
    }
  }
}
